package com.jkd.utils;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.jkd.domain.WarningContacts;

import java.util.ArrayList;
import java.util.List;

//发送短信  告知设备监测信息异常
public class SendInfo {

    private static final String AccessKeyId = "LTAIh5198YIAeHTj";
    private static final String AccessKeySecret = "BX9sWgenS5znmuVyGEqzlCchGhBRPd";
    private static final String SignName = "天合元创";//使用的签名
    private static final String TemplateCode = "SMS_210066581";//发送短信使用的模板

    private static IAcsClient acs = null;//服务对象
    private static SendSmsRequest req = new SendSmsRequest();//短信发送请求对象

    private static List<String> sentSms = new ArrayList<>();//发送过异常的联系人集合

    static {
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", AccessKeyId, AccessKeySecret);
        acs = new DefaultAcsClient(profile);
    }

    public static void sendSms(List<WarningContacts> list) {
        for (WarningContacts warningContacts : list) {
            //FIXME  发送之后是否持续发送的问题
            if (!sentSms.contains(warningContacts.getTel())) {
                sentSms.add(warningContacts.getTel());
                JSONObject obj = new JSONObject();
                obj.put("name", warningContacts.getName());
                obj.put("time", DateUtil.now());
                req.setPhoneNumbers(warningContacts.getTel());//设置接收短信手机号
                req.setSignName(SignName);//设置使用签名
                req.setTemplateCode(TemplateCode);//设置使用通知模板id
                req.setTemplateParam(obj.toJSONString());//设置请求参数 以json字符串形式与模板一致
                SendSmsResponse res = null;//向服务器发送请求
                try {
                    res = acs.getAcsResponse(req);
                } catch (ClientException e) {
                    e.printStackTrace();
                }
                //System.out.println("res code: " + res.getCode());//响应状态码
                // System.out.println("res message: " + res.getMessage());//响应信息
                if (res.getCode() == null && !res.getCode().equals("OK")) {
                    System.out.println(res.getMessage());
                }
            }
        }
//        System.out.println("预警信息=================" + modelNum + "," + sensorModel + "," + sensorNum + "," + warningValue);
    }
}
